﻿using Microsoft.SqlServer.Server;
using BoAoCoffee.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BoAoCoffee
{
    public class SocketClients
    {
        private static SocketClients instance;
        private SocketClients() { }
        public static SocketClients Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SocketClients();
                }
                return instance;
            }
        }
        /// <summary>
        /// 创建客户端
        /// </summary>
        public Socket client;
        public void socketclient()
        {
            try
            {
                ///创建客户端
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                ///IP地址
                IPAddress ip = IPAddress.Parse(IPorPortInfo.Instance.CoffeeIP);
                ///端口号
                IPEndPoint endPoint = new IPEndPoint(ip, 8885);
                ///建立与服务器的远程连接
                client.Connect(endPoint);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 4;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                ///线程问题
                Thread thread = new Thread(ReciveMsg);
                thread.IsBackground = true;
                thread.Start(client);
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 客户端接收到服务器发送的消息
        /// </summary>
        /// <param name="o">客户端</param>
        void ReciveMsg(object o)
        {
            Socket client = o as Socket;
            while (true)
            {
                try
                {
                    ///定义客户端接收到的信息大小
                    byte[] arrList = new byte[1024 * 1024];
                    ///接收到的信息大小(所占字节数)
                    int length = client.Receive(arrList);
                    string msg = Encoding.UTF8.GetString(arrList, 0, length);
                    var ClientList = JsonConvert.DeserializeObject<ClientList>(msg);
                    if (ClientList != null)
                    {
                        if (ClientList.code == 0)
                        {
                            show(ClientList.message.type, ClientList.message.Name);
                        }
                        else if (ClientList.code == 3)
                        {
                            RobotShow(ClientList.message.Name, ClientList.message.type);
                            ClientList clientList = new ClientList();
                            clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                            clientList.robotMsg = RobotMsg.Instance;
                            clientList.code = 3;
                            clientList.IsMake = MainWindow.isMake;
                            var list = JsonConvert.SerializeObject(clientList);
                            var msgs = Encoding.UTF8.GetBytes(list);
                            client.Send(msgs);
                        }
                        else if (ClientList.code == 6)
                        {
                            MainWindow.MakingTypes = ClientList.message.type;
                            MainWindow.img_path = ClientList.message.Name;
                        }
                    }
                }
                catch (Exception)
                {
                    ///关闭客户端
                    client.Close();
                }

            }
        }
        public void show(int code, string msg)
        {
            try
            {
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                switch (msg)
                {
                    case "TGS_BTN":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                          
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);

                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "TGX_BTN":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    case "LB_BTN":
                        if (code == 0)
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_start);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        else
                        {
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(100);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_end);
                            Thread.Sleep(100);
                            IOMsg.Instance.AllBol = true;
                        }
                        break;
                    default:
                        break;
                }
                Thread.Sleep(100);
                IOMsg.Instance.AllBol = true;
                ClientList clientList = new ClientList();
                MessageClientList coffeelist = MessageClientList.Instance;
                coffeelist.Name = msg;
                coffeelist.type = code == 1 ? 0 : 1;
                clientList.message = coffeelist;
                clientList.code = 0;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        ///   状态  0 关状态  1 开状态 2 已开机 3 上电 4 下电 5 已使能 6 下使能 7 已运行 8 继续运行 9 已报警 10 已暂停  11已停止 ,12 机械臂状态查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Type"></param>
        public void RobotShow(string name, int Type)
        {
            try
            {
                switch (Type)
                {
                    case 3:
                        jakaAPI.power_on(ref RobotOperate.handle1);
                        break;
                    case 4:
                        jakaAPI.power_off(ref RobotOperate.handle1);
                        break;
                    case 5:
                        jakaAPI.enable_robot(ref RobotOperate.handle1);
                        break;
                    case 6:
                        jakaAPI.disable_robot(ref RobotOperate.handle1);
                        break;
                    case 7:
                        jakaAPI.program_run(ref RobotOperate.handle1);
                        break;
                    case 8:
                        jakaAPI.program_resume(ref RobotOperate.handle1);
                        break;
                    case 9:
                        jakaAPI.clear_error(ref RobotOperate.handle1);
                        break;
                    case 10:
                        jakaAPI.program_pause(ref RobotOperate.handle1);
                        break;
                    case 11:
                        jakaAPI.program_abort(ref RobotOperate.handle1);
                        break;
                }
                RobotOperate.Robot_L_Show();
            }
            catch (Exception)
            {
            }
        }
    }
}
